package com.example.weatherforecast.whtrcdn.adapter

import android.app.AlertDialog
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.weatherforecast.R
import com.example.weatherforecast.data.city.persistence.entity.CityInfo
import com.example.weatherforecast.whtrcdn.WhtrcdnViewModel
import com.example.weatherforecast.whtrcdn.constant.WhtrcdnConstant
import com.example.weatherforecast.whtrcdn.entity.WeatherCityInfo

class WeatherCitySearchResultListAdapter(private val context: Context, private var data: List<CityInfo>, val viewModel: WhtrcdnViewModel): RecyclerView.Adapter<WeatherCitySearchResultListAdapter.MyViewHolder>() {

    val TAG = WeatherCitySearchResultListAdapter::class.java.simpleName
    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val cityNameLabel: TextView = itemView.findViewById(R.id.tv_weather_city_name_label)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.weather_city_search_list_item, parent, false))
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.cityNameLabel.text = data[position].cityName
        holder.cityNameLabel.setOnClickListener{
            AlertDialog.Builder(context)
                .setTitle("添加提示")
                .setMessage("是否添加城市：" + data[position].cityName)
                .setPositiveButton("确定") { _, _ ->
                    run {
                        val cityList = viewModel.getCurrentWeatherCityInfoList()

                        if (viewModel.checkCurrentWeatherCityDuplicated(cityList, data[position].cityName)){
                            Toast(context).also {
                                it.setText("该城市已添加: " + data[position].cityName)
                                it.duration = Toast.LENGTH_SHORT
                                it.show()
                            }
                            return@setPositiveButton
                        }

                        if (cityList.size > WhtrcdnConstant.CITY_COUNT_LIMIT){
                            Toast.makeText(context, "城市数量已达到上限，最多${WhtrcdnConstant.CITY_COUNT_LIMIT}个", Toast.LENGTH_SHORT).show()
                        }else {
                            cityList.add(WeatherCityInfo(data[position].cityName, data[position].cityId, 0))
                            viewModel.updateCurrentWeatherCityInfoList(cityList)
                            Toast.makeText(context, "添加成功: " + data[position].cityName, Toast.LENGTH_SHORT).show()
                        }
                    }
                }
                .setNegativeButton("取消") { _, _ ->
                    run {

                    }
                }.show()
        }
    }

    override fun getItemCount(): Int {
        Log.i(TAG, "getItemCount: " + data.size)
        return data.size
    }

    fun updateData(cityInfoList: List<CityInfo>){
        data = cityInfoList
    }

}